<?php
class WsShare extends AppModel {
	var $name = 'WsShare';
	var $useDbConfig = 'ws';
	var $primaryKey = 'LineNo';
	var $displayField = 'RegdName1';
        var $order = "CommonID DESC";
	var $validate = array(
		'LineNo' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'CommonID' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'CompanyName' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'CompanyCode' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'RegdName1' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
        
        
        
    function import($filename) {
        $error = false;

        // open the file
        $handle = fopen($filename, "r");

        // read the 1st row as headings
        $header = fgetcsv($handle);

        // create a message container
        $return = array(
            'messages' => array(),
            'errors' => array(),
        );
        $i = 0;
        // read each data row in the file
        while (($row = fgetcsv($handle)) !== FALSE) {
            $i++;
            $data = array();

            // for each header field 
            foreach ($header as $k => $head) {
                // get the data field from Model.field
                //echo $head;exit;
                if (strpos($head, '.') !== false) {
                    $h = explode('.', $head);
                    $data[$h[0]][$h[1]] = (isset($row[$k])) ? $row[$k] : '';
                }
                // get the data field from field
                else {
                    $data['WsShare'][$head] = (isset($row[$k])) ? $row[$k] : '';
                }
            }

            // see if we have an id 			
            $id = isset($data['WsShare']['LineNo']) ? $data['WsShare']['LineNo'] : 0;
            if($data['WsShare']['Date']!=""){
               $data['WsShare']['Date']=date("Y-m-d H:i:s",strtotime($data['WsShare']['Date']));
               $data['WsShare']['CommonID']=strtotime($data['WsShare']['Date']);                
            }else{
                $data['WsShare']['CommonID']=strtotime($data['WsShare']['DateDocRecd']);            
            }
            // we have an id, so we update
            if ($id) {
                // there is 2 options here, 
                // option 1:
                // load the current row, and merge it with the new data
                //$this->recursive = -1;
                //$post = $this->read(null,$id);
                //$data['Post'] = array_merge($post['Post'],$data['Post']);
                // option 2:
                // set the model id
                $this->LineNo = $id;
            }
            // or create a new record
            else {
                $this->create();
            }

            // see what we have
           //debug($data);exit;
          //  pr($data);
            // validate the row
            $this->set($data);
            $this->save($data);
            //if($i==10){echo"sahabj"; break;}
        }

        // close the file
        fclose($handle);

        // return the messages
        return $return;
    }
}
